package com.gitee.wsl.mathematics.geometry.d2.rect.ext

import com.gitee.wsl.mathematics.coordinate.d2.Coordinate2
import com.gitee.wsl.mathematics.geometry.d2.rect.RectangleShape2d
import com.gitee.wsl.mathematics.number.compareTo

/** Fits this rectangle around another rectangle while maintaining aspect ratio. This scales and centers the rectangle to the
 * other rectangle (e.g. Having a camera translate and scale to show a given area)
 * @param rect the other rectangle to fit this rectangle around
 * @return this rectangle for chaining
 */
fun<N:Number,V: Coordinate2<N, V>,R: RectangleShape2d<N, V, R>> R.fitOutside(rect: RectangleShape2d<N, *, *>): R {
    val ratio = aspectRatio
    val w: N
    val h: N
    if (ratio > rect.aspectRatio) { // Wider than tall
        w = rect.height * ratio
        h = rect.height
    } else { // Taller than wide
        w = rect.width
        h = rect.width / ratio
    }
    return createWH(rect.x + rect.width / 2 - width / 2, rect.y + rect.height / 2 - height / 2,w,h)
}

/** Fits this rectangle into another rectangle while maintaining aspect ratio. This scales and centers the rectangle to the
 * other rectangle (e.g. Scaling a texture within a arbitrary cell without squeezing)
 * @param rect the other rectangle to fit this rectangle inside
 * @return this rectangle for chaining
 */
fun<N:Number,V: Coordinate2<N, V>,R: RectangleShape2d<N, V, R>> R.fitInside(rect: RectangleShape2d<N, *, *>): R {
    val ratio = aspectRatio
    val w: N
    val h: N
    if (ratio > rect.aspectRatio) { // Wider than tall
        w = rect.height * ratio
        h = rect.height
    } else { // Taller than wide
        w = rect.width
        h = rect.width / ratio
    }
    return createWH(rect.x + rect.width / 2 - width / 2, rect.y + rect.height / 2 - height / 2,w,h)
}